package 晚间作业0316.Doc12_实战案例二;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * 【难】如下字符串： 01#张三#20*02#李四#18*03#王五#22*04#赵六#20*05#田七#21
 * 创建Student类，属性：学号 姓名 年龄
 * 1.将这些Student对象放进数组中
 * 2.查询里面有没有叫田七的学生如果存在则打印学生的具体信息（封装成方法，键盘输入姓名） 123456789
 * 3.计算所有学生的平均年龄，最大年龄，最小年龄
 * 4.如果存在名字叫张三的将名字改为张兵
 */
public class Doc12_实战案例二 {

    public static void main(String[] args) {
        StuManageSystem system = new StuManageSystem("01#张三#20*02#李四#18*03#王五#22*04#赵六#20*05#田七#21");
        system.searchByInputName();
        system.ageAnalyze();

        if (system.nameChange("张三", "张兵")) {
            System.out.println("查找成功");
            system.printInfos();
        } else {
            System.out.println("未找到");
        }
    }

    private static class StuManageSystem {
        private List<Student> studentList;

        public StuManageSystem(String students) {
            String[] split = students.split("\\*");
            studentList = new ArrayList<>(split.length);
            for (String studentStr : split) {
                studentList.add(Student.trans2Student(studentStr));
            }
        }

        public void searchByInputName() {
            System.out.println("请输入要查找的学生姓名: ");
            Scanner scanner = new Scanner(System.in);
            String sName = scanner.next();
            searchStudentsByName(sName).forEach(System.out::println);
        }

        public List<Student> searchStudentsByName(String name) {
            return studentList.stream().filter(student -> name.equals(student.name)).collect(Collectors.toList());
        }

        public void ageAnalyze() {
            int min = studentList.get(0).age;
            int max = min;
            int sum = min;
            Student student;
            for (int i = 1; i < studentList.size(); i++) {
                student = studentList.get(i);
                if (student.age > max) {
                    max = student.age;
                }
                if (student.age < min) {
                    min = student.age;
                }
                sum += student.age;
            }
            System.out.println("平均年龄：" + (sum / studentList.size()) + ", 最大年龄：" + max + ", 最小年龄：" + min);
        }

        public boolean nameChange(String old, String replace) {
            List<Student> students = searchStudentsByName(old);
            if (students != null && students.size() > 0) {
                students.forEach(item -> item.name = replace);
                return true;
            }
            return false;
        }

        public void printInfos() {
            System.out.println(studentList);
        }
    }

    private static class Student {
        private String sNumber;
        private String name;
        private int age;

        public Student(String sNumber, String name, int age) {
            this.sNumber = sNumber;
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "学生信息：{" +
                    "学号='" + sNumber + '\'' +
                    ", 姓名='" + name + '\'' +
                    ", 年龄=" + age +
                    '}';
        }

        public static Student trans2Student(String studentStr) {
            String[] split = studentStr.split("#");
            return new Student(split[0], split[1], Integer.parseInt(split[2]));
        }
    }
}
